import store from '@/store'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Main',
    component: () => import('../views/Main.vue'),
    redirect:'/login',
    children:[
      /* {
        path: '/home',
        name: 'home',
        component: () => import('../views/home'),
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('../views/user'),
      },
      {
        path: '/mall',
        name: 'mall',
        component: () => import('../views/Mall/index.vue'),
      },
      {
        path: '/page1',
        name: 'page1',
        component: () => import('../views/Other/PageOne.vue'),
      },
      {
        path: '/page2',
        name: 'page2',
        component: () => import('../views/Other/PageTwo.vue'),
      }, */
    ]
  },
  {
    path: '/',
    name: 'test',
    component: () => import('../views/test/a.vue'),
    children:[
      {
        path: '/b',
        name: 'b',
        component: () => import('../views/test/b.vue'),
      }
    ]},
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/login.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  store.commit('getToken')
  const token=store.state.token
  if(!token && to.name!=='login'){
    next({name:'login'})
  }else if(token && to.name=='login'){
    next({name:'home'})
  }
  next()
})
export default router
